package arena

import (
	"context"
	"fmt"
	log2 "github.com/name5566/leaf/log"
	"go.mongodb.org/mongo-driver/bson"
	"server/db"
	"time"
)

const (
	ArenaCollection     = "t_arena"
	ArenaMsgCollection  = "t_arena_msg"
	ArenaShopCollection = "t_arena_shop"
	BattleCollection    = "t_battle"
)

type ArenaDao struct {
	db *db.MongoMiddleware
}

func NewArenaDao() *ArenaDao {
	return &ArenaDao{
		db: db.Db,
	}
}

// 获取竞技场玩家总数
func (dao *ArenaDao) GetCount() (int64, error) {
	var count int64
	filter := bson.M{"uid": bson.M{"$ne": ""}}
	count, err := dao.db.Client.Collection(ArenaCollection).Find(context.Background(), filter).Count()
	if err != nil {
		fmt.Println(err)
		return 0, nil
	}
	return count, err
}
func (dao *ArenaDao) GetShopList(uid string) []ArenaShopInfo {
	filter := bson.M{"user_id": uid}
	var results []ArenaShopInfo
	err := dao.db.Find(ArenaShopCollection, filter, &results)
	if err != nil {
		log2.Error("find is wrong %v", err)
	}
	return results
}
func (dao *ArenaDao) GetShopInfoByItemId(uid string, itemId int) (ArenaShopInfo, error) {
	filter := bson.M{"user_id": uid, "item_id": itemId}
	var results ArenaShopInfo
	err := dao.db.FindOne(ArenaShopCollection, filter, &results)
	return results, err
}

// 获取多个玩家的信息
func (dao *ArenaDao) GetArrInfo(arrUid []int64) ([]Arena, error) {
	filter := bson.M{"uid": bson.M{"$in": arrUid}}
	var results []Arena
	err := dao.db.Find(ArenaCollection, filter, &results)
	return results, err
}

// 获取多个玩家的信息
func (dao *ArenaDao) GetArrByPos(arrUid []int) ([]Arena, error) {
	filter := bson.M{"position": bson.M{"$in": arrUid}}
	var results []Arena
	err := dao.db.Find(ArenaCollection, filter, &results)
	return results, err
}

// 获取最高排名
func (dao *ArenaDao) GetMaxPosition() (int, error) {
	filter := []bson.M{
		{"$sort": bson.M{"position": -1}},
		{"$limit": 1},
	}
	var result Arena
	err := dao.db.AggregateOne(ArenaCollection, filter, &result)
	if err != nil {
		log2.Error("errmsg is %v", err)
		return 0, nil
	}
	return result.Position, nil
}

// 插入新玩家数据
func (dao *ArenaDao) Insert(uid string, data Arena) error {
	data.UID = uid
	return dao.db.Insert(ArenaCollection, data)
}
func (dao *ArenaDao) InsertShop(uid string, data ArenaShopInfo) error {
	data.UserId = uid
	return dao.db.Insert(ArenaShopCollection, data)
}

// 获取指定玩家的信息
func (dao *ArenaDao) Get(uid string) (*Arena, error) {
	filter := bson.M{"uid": uid}
	var result Arena
	err := dao.db.FindOne(ArenaCollection, filter, &result)
	return &result, err
}

// 按排名获取玩家信息
func (dao *ArenaDao) GetByPosition(position int, arrField []string) (*Arena, error) {
	filter := bson.M{"position": position}
	var result Arena
	err := dao.db.FindOne(ArenaCollection, filter, &result)
	return &result, err
}

// 更新玩家数据
func (dao *ArenaDao) Update(uid string, updateData bson.M) error {
	filter := bson.M{"uid": uid}
	return dao.db.Update(ArenaCollection, filter, bson.M{"$set": updateData})
}
func (dao *ArenaDao) UpdateShop(uid string, itemId int, updateData bson.M) error {
	filter := bson.M{"uid": uid, "item_id": itemId}
	return dao.db.Update(ArenaShopCollection, filter, bson.M{"$set": updateData})
}

// 交换竞技场位置
func (dao *ArenaDao) UpdateChallenge(info, atkedInfo Arena) error {
	posTmp := 100000000 + time.Now().Unix()%100000000
	//posTmp := info.Position
	filter1 := bson.M{"uid": info.UID}
	update1 := bson.M{"$set": bson.M{"position": posTmp}}
	err := dao.db.Update(ArenaCollection, filter1, update1)
	if err != nil {
		return err
	}

	filter2 := bson.M{"uid": atkedInfo.UID}
	update2 := bson.M{"$set": bson.M{"position": atkedInfo.Position}}
	if err := dao.db.Update(ArenaCollection, filter2, update2); err != nil {
		return err
	}

	update3 := bson.M{"$set": bson.M{"position": info.Position}}
	return dao.db.Update(ArenaCollection, filter1, update3)
}

// 获取排名列表
func (dao *ArenaDao) GetPositionList(num int, arrField []string) ([]Arena, error) {
	filter := bson.M{"position": bson.M{"$lte": num}}
	var results []Arena
	err := dao.db.Find(ArenaCollection, filter, &results)
	return results, err
}

// 获取竞技场战报
func (dao *ArenaDao) GetMsg(uid string) ([]ArenaMsg, error) {
	filter := bson.M{"$or": []bson.M{{"attack_uid": uid}, {"defend_uid": uid}}}
	var results []ArenaMsg
	err := dao.db.Find(ArenaMsgCollection, filter, &results)
	return results, err
}

// 插入战报
func (dao *ArenaDao) InsertMsg(msg ArenaMsg) error {
	return dao.db.Insert(ArenaMsgCollection, msg)
}
func (dao *ArenaDao) InsertBattleMsg(msg BattleMsgList) error {
	return dao.db.Insert(ArenaMsgCollection, msg)
}

// 获取前 N 名玩家
func (dao *ArenaDao) GetTop(offset, limit int, arrField []string) ([]ArenaMsg, error) {
	filter := bson.M{"uid": bson.M{"$gt": 0}}
	var results []ArenaMsg
	err := dao.db.Find(ArenaCollection, filter, &results)
	return results, err
}
func (dao *ArenaDao) UpdateReward(uid string) error {
	// 计算 rewardTime
	strRewardTime := fmt.Sprintf("-%d day", LAST_DAYS)
	duration, err := time.ParseDuration(strRewardTime)
	if err != nil {
		return err
	}
	rewardTime := time.Unix(time.Now().Unix(), 0).Add(duration)

	// 获取集合
	collection := dao.db

	// 定义更新条件
	filter := bson.M{
		"uid":         uid,
		"reward_time": bson.M{"$gt": rewardTime},
	}

	// 定义更新内容
	update := bson.M{
		"$set": bson.M{
			"va_reward": []interface{}{},
		},
	}

	// 执行更新操作
	err = collection.Update(ArenaCollection, filter, update)
	if err != nil {
		return err
	}

	return nil
}

// GetCount 假设该方法从数据库或其他数据源获取计数
